package com.itboye.mengwu.luntan.comment.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aizhiwu.GeRenActivity;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReplyBaoBaoLe;
import com.itboye.mengwu.data.model.TieZiModel;
import com.itboye.mengwu.luntan.comment.bean.Comment;
import com.itboye.mengwu.luntan.comment.view.MyListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CommentAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private Context context;
	private OnClickListener replyToCommentListener;
	private OnClickListener replyToReplyListener;
	private OnClickListener dianzanListener;
	private ArrayList<TieZiBaoHanReplyBaoBaoLe.GetAList> commentList;
	private OnClickListener guanzhuOnclickListener;
	int i;

	public CommentAdapter(
			Context context,
			ArrayList<TieZiBaoHanReplyBaoBaoLe.GetAList> commentList,
			// OnClickListener replyToCommentListener,
			CommentReplyAdapter myAdapter,
			OnClickListener replyToReplyListener,
			OnClickListener dianzanListener,
			OnClickListener guanzhuOnclickListener) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.commentList = commentList;
		// this.replyToCommentListener = replyToCommentListener;
		this.replyToReplyListener = replyToReplyListener;
		this.guanzhuOnclickListener = guanzhuOnclickListener;
		this.dianzanListener = dianzanListener;
	}

	public void clearList() {
		this.commentList.clear();
	}

	public void updateList(
			ArrayList<TieZiBaoHanReplyBaoBaoLe.GetAList> commentList) {
		this.commentList.addAll(commentList);
	}

	@Override
	public int getCount() {
		return commentList.size();
	}

	@Override
	public TieZiBaoHanReplyBaoBaoLe.GetAList getItem(int position) {
		return commentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list, null);
			viewHolder = new ViewHolder();
			// viewHolder.iv_user_photo = (ImageView) convertView
			// .findViewById(R.id.iv_user_photo);
			viewHolder.tv_user_name = (TextView) convertView
					.findViewById(R.id.tv_title);
			viewHolder.tv_user_comment = (TextView) convertView
					.findViewById(R.id.tv_content);
			viewHolder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			viewHolder.btn_comment_reply = (TextView) convertView
					.findViewById(R.id.tetPing);
			viewHolder.txt_time = (TextView) convertView
					.findViewById(R.id.textTime);
			viewHolder.lv_user_comment_replys = (MyListView) convertView
					.findViewById(R.id.lv_user_comment_replys);
			viewHolder.tetzan = (TextView) convertView
					.findViewById(R.id.tetzan);
			viewHolder.imgZan = (ImageView) convertView
					.findViewById(R.id.imgZan);
			viewHolder.tetPing = (TextView) convertView
					.findViewById(R.id.tetPing);
			viewHolder.gridview = (NoScrollGridView) convertView
					.findViewById(R.id.gridview);
			viewHolder.btn_comment_reply.setTag(position);
			viewHolder.textGuanzhu = (TextView) convertView
					.findViewById(R.id.textGuanzhu);
			viewHolder.imgPing = (ImageView) convertView
					.findViewById(R.id.imgPing);
			viewHolder.pinglunderen = (LinearLayout) convertView
					.findViewById(R.id.pinglunderen);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		i = position;

		final TieZiBaoHanReplyBaoBaoLe.GetAList comment = getItem(position);
		viewHolder.tv_user_name.setText(comment.getauthor());
		viewHolder.tv_user_comment.setText("\t"
				+ commentList.get(position).getMessage());
		viewHolder.iv_avatar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, GeRenActivity.class);
				intent.putExtra("uid",
						SPUtils.get(context, null, SpPublicParams.UID, "") + "");
				intent.putExtra("fromUid", comment.getauthorId());
				context.startActivity(intent);
			}
		});
		// 设置评论列表的点击效果透明
		// viewHolder.lv_user_comment_replys.setSelector(new ColorDrawable(
		// Color.TRANSPARENT));
		// viewHolder.txt_time
		// .setText(new SimpleDateFormat("yyyy-MM-dd").format(Long
		// .parseLong(commentList.get(position).getDateline()) * 1000));
		SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy-MM-dd");// 年月日的格式
		SimpleDateFormat myFmt2 = new SimpleDateFormat("HH:mm:ss");// 时分秒的格式
		SimpleDateFormat myFmt3 = new SimpleDateFormat("m分钟前");// 最后做时间差用的分钟
		SimpleDateFormat myFmt4 = new SimpleDateFormat("H小时前");// 最后做时间差用的小时
		// Log.e("!!!!!!", position + "");
		// Log.e("!!!!!!",
		// myFmt1.format(Long.parseLong((aLists.get(position).getDateline()) +
		// "") * 1000));// 获取该position下的帖子年月日
		// Log.e("!!!!!!",
		// myFmt2.format(Long.parseLong((aLists.get(position).getDateline()) +
		// "") * 1000));// 获取该position下的帖子时分秒
		//
		// Log.e("!!!!!!", myFmt1.format(System.currentTimeMillis()));// 获取当前年月日

		// 获取当前年月日
		String nowDay = myFmt1.format(System.currentTimeMillis());
		// 获取该position下的帖子年月日
		String positionDay = myFmt1.format(Long.parseLong((commentList
				.get(position).getDateline()) + "") * 1000);
		// 获取该position下的帖子时分秒
		String positionTime = myFmt2.format(Long.parseLong((commentList
				.get(position).getDateline()) + "") * 1000);
		// 如果是当天显示时和分， 如果超过一天显示日期
		if (nowDay.equals(positionDay)) {
			String result;
			Log.e("--------",
					System.currentTimeMillis()
							- Long.parseLong((commentList.get(position)
									.getDateline()) + "") * 1000 + "");
			// 3600000是一个小时的时间戳
			// 如果大于一个小时只显示小时，如果小与一个小时显示分钟
			if (3600000 > (System.currentTimeMillis() - Long
					.parseLong((commentList.get(position).getDateline()) + "") * 1000)) {
				result = myFmt3.format(System.currentTimeMillis()
						- Long.parseLong((commentList.get(position)
								.getDateline()) + "") * 1000);
			} else {
				result = myFmt4.format(System.currentTimeMillis()
						- Long.parseLong((commentList.get(position)
								.getDateline()) + "") * 1000 - 28800000);
				// Log.e("hu",
				// "System.currentTimeMillis()="
				// + System.currentTimeMillis()
				// + ",Long.parseLong((commentL="
				// + Long.parseLong((commentList.get(position)
				// .getDateline()) + "") * 1000);
			}
			viewHolder.txt_time.setText(result);
		} else {
			viewHolder.txt_time.setText(positionDay);

		}
		Log.e("hu   3", commentList.get(position).getLikeInfo() + "");
		viewHolder.pinglunderen.removeAllViews();
		if (commentList.get(position).getLikeInfo() != null) {
			for (int i = 0; i < commentList.get(position).getLikeInfo().size(); i++) {
				Log.e("hu   3", commentList.get(position).getLikeInfo().size()
						+ "");
				RoundImageView imageView = new RoundImageView(context);
				LayoutParams layoutParams = new LayoutParams(50,
						LayoutParams.MATCH_PARENT);
				layoutParams.setMargins(5, 0, 5, 0);
				if (commentList.get(position).getLikeInfo().get(i).getHead() == null
						|| "".equals(commentList.get(position).getLikeInfo()
								.get(i).getHead())) {
					Log.e("hu2", "sex="
							+ commentList.get(position).getLikeInfo().get(i)
									.getSex());
					if ("1".equals(commentList.get(position).getLikeInfo()
							.get(i).getSex())) {
						imageView.setImageResource(R.drawable.male);
					} else {
						imageView.setImageResource(R.drawable.female);
					}
				} else {
					if (!commentList.get(position).getLikeInfo().get(i)
							.getHead().startsWith("http")) {
						XImageLoader
								.load(GlobalConfig.API_URL
										+ "/picture/index?id="
										+ commentList.get(position)
												.getLikeInfo().get(i).getHead(),
										imageView);
					} else {
						XImageLoader.load(commentList.get(position)
								.getLikeInfo().get(i).getHead(), imageView);
					}

				}
				imageView.setTag(R.id.grid, commentList.get(position)
						.getLikeInfo().get(i).getUid());
				// Log.e("hu  heid", aLists.get(position).getLikeInfo().get(i)
				// .getUid());
				viewHolder.pinglunderen.addView(imageView, layoutParams);
				imageView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// TODO Auto-generated method stub
						Intent intent = new Intent(context, GeRenActivity.class);
						Log.e("hu  tag", v.getTag(R.id.grid) + "");
						intent.putExtra(
								"uid",
								SPUtils.get(context, null, SpPublicParams.UID,
										"") + "");
						intent.putExtra("fromUid", v.getTag(R.id.grid) + "");
						context.startActivity(intent);
					}

				});
			}
		}
		viewHolder.tetPing.setOnClickListener(replyToReplyListener);

		viewHolder.imgZan.setTag(R.id.img1, position);
		viewHolder.imgZan.setTag(R.id.img2, commentList.get(position)
				.getIsLike());
		viewHolder.imgZan.setTag(R.id.img3, commentList.get(position).getTid());
		if ("1".equals(commentList.get(position).getIsLike())) {
			viewHolder.imgZan.setBackgroundResource(R.drawable.like_select);
		} else {
			viewHolder.imgZan.setBackgroundResource(R.drawable.like_unselect);
		}
		// 判断当前评论是否有回复
		System.out.println(commentList.get(position).getReplyInfo().getList()
				+ "这是评论及回复");
		if (commentList.get(position).getReplyInfo().getList() != null) {
			// if(commentList.get(position).getReplyInfo().getList().size()<=0){
			// viewHolder.lv_user_comment_replys.setVisibility(View.GONE);
			// }else{
			viewHolder.lv_user_comment_replys
					.setAdapter(new CommentReplyAdapter(context, commentList
							.get(position).getReplyInfo().getList(), position,
							replyToReplyListener));
			// }
		}
		// else {
		// viewHolder.lv_user_comment_replys.setVisibility(View.GONE);
		// }

		if (commentList.get(position).getIsFocus().equals("0")) {
			viewHolder.textGuanzhu.setText("+关注");
			viewHolder.textGuanzhu.setTextColor(context.getResources()
					.getColor(R.color.main_color));
		} else {
			viewHolder.textGuanzhu.setText("已关注");
			viewHolder.textGuanzhu.setTextColor(Color.BLACK);
		}
		viewHolder.textGuanzhu.setTag(commentList.get(position).getAuthorId());
		viewHolder.textGuanzhu.setTag(R.id.tag_first, commentList.get(position)
				.getIsFocus());
		viewHolder.textGuanzhu.setOnClickListener(guanzhuOnclickListener);
		viewHolder.imgZan.setOnClickListener(dianzanListener);
		viewHolder.tetzan.setText(commentList.get(position).getLikes());
		// 记录点击回复按钮时对应的position,用于确定所回复的对象
		viewHolder.imgPing.setTag(position);
		viewHolder.tetPing.setText(commentList.get(position).getReplyInfo()
				.getCount());
		viewHolder.btn_comment_reply.setTag(position);
		viewHolder.imgPing.setOnClickListener(replyToCommentListener);
		viewHolder.btn_comment_reply.setOnClickListener(replyToCommentListener);
		final ArrayList<String> arrayImage = commentList.get(position).getImg();
		// if (arrayImage == null || arrayImage.size() == 0) { //
		// 没有图片资源就隐藏GridView
		// viewHolder.gridview.setVisibility(View.GONE);
		// } else {
		viewHolder.gridview.setAdapter(new NoScrollGridAdapter(context,
				arrayImage));
		// }
		// 使用ImageLoader加载网络图片
		if ("".equals(commentList.get(position).getHead())
				|| commentList.get(position).getHead() == null) {
			if ("1".equals(commentList.get(position).getSex())) {
				viewHolder.iv_avatar.setImageResource(R.drawable.male);
			} else {
				viewHolder.iv_avatar.setImageResource(R.drawable.female);
			}

		} else {
			if (!commentList.get(position).getHead().startsWith("http")) {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ commentList.get(position).getHead(),
						viewHolder.iv_avatar);
			} else {
				XImageLoader.load(commentList.get(position).getHead(),
						viewHolder.iv_avatar);
			}

		}

		// 点击回帖九宫格，查看大图
		viewHolder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, arrayImage);
			}
		});
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		context.startActivity(intent);
	}

	public class ViewHolder {
		private ImageView iv_avatar, imgZan, imgPing;
		private TextView txt_time;
		private ImageView iv_user_photo; // 评论者 头像
		private TextView tv_user_name; // 评论者 昵称
		private TextView tv_user_comment; // 评论者 一级品论内容
		// private TextView tv_user_comment_date; //
		private TextView btn_comment_reply; // 评论者 二级评论按钮
		private MyListView lv_user_comment_replys; // 评论者 二级品论内容列表
		private NoScrollGridView gridview;
		TextView tetzan, textGuanzhu, tetPing;
		LinearLayout pinglunderen;
	}
}
